<template>
  <div class="from">
    <van-nav-bar
      title="修改密码"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-field
      name="asyncValidator"
      v-model.trim="oldPassword"
      placeholder="请输入原始密码"
    />
    <van-field
      name="asyncValidator"
      v-model.trim="password"
      placeholder="请输入新密码"
    />
    <van-field
      name="asyncValidator"
      v-model.trim="rePasswoed"
      placeholder="请确认新密码"
    />
    <div style="margin: 16px;">
      <van-button round block type="info" native-type="submit" @click="save"
        >提交</van-button
      >
    </div>
  </div>
</template>

<script>
//修改密码
import { modifyPwd } from "../services/users";
import { removeToken } from "../utils/tools";
import { Notify } from "vant";
export default {
  name: "UserPwd",
  data() {
    return {
      oldPassword: "",
      password: "",
      rePasswoed: "",
    };
  },
  methods: {
    async save() {
      if (this.password) {
        if (this.password && this.password === this.rePasswoed) {
          //修改密码
          const res = await modifyPwd({
            password: this.password,
            oldPassword: this.oldPassword,
          });
          if (res.code === 1) {
            Notify({ type: "success", message: "密码修改成功", duration: 500 });
            removeToken();
            this.$router.push({
              name: "Login",
            });
          } else {
            // alert(res.msg);
            Notify(res.msg);
          }
          console.log(res);
        } else {
          Notify("两次输入的密码不一致，请仔细检查后重新输入");
          // alert("两次输入的密码不一致，请仔细检查后重新输入");
        }
      } else {
        Notify("密码不能为空");
        // alert("密码不能为空");
      }
    },
    onClickLeft() {
      this.$router.push({
        name: "User",
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
